package com.ghy.springredis.lettuce;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author: 卢佳新
 * @version: v1.0
 * @description: com.ghy.springredis.lettuce
 * @date:2022/6/11 14:55
 * @Motto 不积跬步，无以至千里；不积小流，无以成江海。
 */
//@Configuration
//@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * RedisTemplate配置
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
        // 配置redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer ());//key序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer ());//value序列化
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}
